package com.szly.phm.admin.utils;

import com.szly.phm.common.entity.result.RootResponse;
import org.aspectj.weaver.ast.Test;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.Map;

@RestControllerAdvice()
    public class CustomResponseAdvice implements ResponseBodyAdvice {
        @Override
        public boolean supports(MethodParameter methodParameter, Class aClass) {
            System.out.println("Is supporting method");

            //true  支持; false   不支持
            return methodParameter.hasMethodAnnotation(CustomResponse.class);
        }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        System.out.println("In beforeBodyWrite() method of " + getClass().getSimpleName());
        if (o instanceof Test) {
            return o;
        }
        var loginUser = SecurityUtils.getLoginUser();
        //这里可以针对返回结果进行处理，比如加密等
        ((RootResponse)o).setNewToken(loginUser.getNewToken());
        return o;
    }
}


